package com.cloudpivot.receivales.contract.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.contract.domain.NormalReturn;
import com.cloudpivot.receivales.contract.domain.bpm.BpmContractMain;
import com.cloudpivot.receivales.contract.domain.oa.OAContract;
import com.cloudpivot.receivales.contract.service.ContractAffairsService;
import com.cloudpivot.receivales.contract.service.http.HttpAPIServiceForCrm;
import com.cloudpivot.receivales.contract.service.impl.*;
import com.cloudpivot.receivales.contract.util.CompanyEnum;
import com.cloudpivot.receivales.util.DingMessageUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取OA接口
 */
@Controller
@Slf4j
@RequestMapping("/contractAffairs")
public class ContractAffairsController {

    @Autowired
    private DingMessageUtil dingMessageUtil;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    @Autowired
    private ContractAffairsService contractAffairsService;

    @Value("${myprops.cpurl}")
    private String BPMURL;

    /**
     * 为V3报价准备的全新合同明细替换接口服务
     */
    @PostMapping(value = "/contractReplaceToSystemV3")
    @ResponseBody
    public NormalReturn contractReplaceToSystemV3(@Param("objId") String objId){
        log.info("contractReplaceToSystemV3打印获取到的合同json:{}", objId);

        if (StringUtils.isBlank(objId)) {
            return new NormalReturn(0, "合同推送异常");
        }
        String ret= contractAffairsService.pushContractReplaceToCrm(objId);
        if (StringUtils.isBlank(ret)) {
            dingMessageUtil.pushMessage(null,"替换合同信息失败(只推送CRM) 请查看日志,对应objId==>" + objId);
            return new NormalReturn(0, "替换合同失败", "", null);
        }else{
            dingMessageUtil.pushMessage(null,"替换合同信息成功(只推送CRM),对应objId==>" + objId);
            return new NormalReturn(1, "合同替换成功", "", null);
        }

    }

    /**
     * 为V3报价准备的全新的合同追加接口服务
     * 当前该接口 只更新crm合同信息
     * @return
     */
    @PostMapping(value = "/contractAppendToSystemV3")
    @ResponseBody
    public NormalReturn contractAppendToSystemV3(@Param("objId") String objId) {
        log.info("打印获取到的合同json:{}", objId);
        if (StringUtils.isBlank(objId)) {
            return new NormalReturn(0, "合同推送异常");
        }
        String ret=contractAffairsService.pushContractAppendToCrm(objId);
        if (StringUtils.isBlank(ret)) {
            dingMessageUtil.pushMessage(null,"追加合同信息失败(只推送CRM),对应objId==>" + objId);
            return new NormalReturn(0, "追加合同失败", "", null);
        }else{
            dingMessageUtil.pushMessage(null,"追加合同信息成功(只推送CRM),对应objId==>" + objId);
            return new NormalReturn(1, "追加合同成功", "", null);
        }
    }


    /**
     * 为V3报价准备的全新的合同推送接口
     * 当前该接口 只推送至CRM系统
     * @return
     */
    @PostMapping(value = "/pushContractToSystemV3")
    @ResponseBody
    public NormalReturn pushContractToSystemV3(@Param("objId") String objId) {
        log.info("打印获取到的合同json:{}", objId);
        if(StringUtils.isBlank(objId)){
            return new NormalReturn(0, "合同推送异常");
        }
        //获取合同对象+对象转换+推送crm合同
        String crmContractGuid=contractAffairsService.pushContractToCrm(objId);

        //其他系统.....

        //直接返回结果
        //合同同步记录器
        Map<String, String> bpmObject = new HashMap<>();
        bpmObject.put("erpid", null);
        bpmObject.put("crmid", crmContractGuid);
        String middel=updateBpmContract(bpmObject);
        bpmObject.put("cpId",middel);
        dingMessageUtil.pushMessage(null,"推送合同成功(只推送CRM),对应objId==>" + objId);
        return new NormalReturn(1, "推送合同成功", "", bpmObject);
    }

    @PostMapping(value = "/updateContractToSystemV3")
    @ResponseBody
    public NormalReturn updateContractToSystemV3(@Param("objId") String objId,@Param("crmGuid") String crmGuid) {
        if(StringUtils.isBlank(objId)||StringUtils.isBlank(crmGuid)){
            return new NormalReturn(0, "合同推送异常");
        }
        String bpmObject=contractAffairsService.updateContractAppendToCrmForSingleField(objId,crmGuid);
        return new NormalReturn(1,"合同更新成功",bpmObject);
    }


    public String updateBpmContract(Map<String,String> retMap){
        String reqUrl=String.format("%s%s", BPMURL, "/api/outservice/addcontract");
        try {
            String ret = httpAPIServiceForCrm.doPost(reqUrl, JSONObject.toJSONString(retMap), null);
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("返回合同同步记录器异常");
            return null;
        }
    }

}
